﻿package com.tudou.newPlayer
{
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.utils.*;

    public class VideoPlayer extends BaseVideoPlayer
    {
        protected var _orginalVideoScale:Number = 1.33333;
        protected var _videoScale:Number;
        private var maskTimer:Timer;
        private var masksContainer:Sprite;
        protected var rectMaskArr:Array;
        private var maskArr:Array;
        private var _rotateType:int = 0;
        protected var hasLogo:Boolean = false;
        protected var _alwaysCheckPolicyFile:Boolean = false;
        protected var _brightness:Number = 0;
        protected var _fullFillVideo:Boolean = false;
        private static const MASK_WIDTH:uint = 100;
        private static const CHILD_MASK_LENGTH:uint = 8;
        private static const BLURXY:uint = 16;
        private static const MASK_HEIGHT:uint = 75;
        private static const RENDER_INTERVAL:uint = 200;

        public function VideoPlayer(param1:uint = 400, param2:uint = 300)
        {
            _videoScale = _orginalVideoScale;
            masksContainer = RectDisplayUtil.getSprite(param1, param2, 16711680, 0);
            var _loc_3:* = RectDisplayUtil.getShape(param1, param2, 65280, 0);
            masksContainer.mask = _loc_3;
            masksContainer.addChild(_loc_3);
            super(param1, param2);
            addChild(masksContainer);
            maskArr = [];
            rectMaskArr = [];
            maskTimer = new Timer(RENDER_INTERVAL);
            maskTimer.addEventListener(TimerEvent.TIMER, renderMask);
            var _loc_4:Boolean = true;
            this.buttonMode = true;
            this.useHandCursor = _loc_4;
            return;
        }// end function

        public function set alwaysCheckPolicyFile(param1:Boolean) : void
        {
            _alwaysCheckPolicyFile = param1;
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            background.width = param1;
            refresh();
            return;
        }// end function

        public function get videoScale() : Number
        {
            return _videoScale;
        }// end function

        override public function set scaleX(param1:Number) : void
        {
            background.scaleX = param1;
            refresh();
            return;
        }// end function

        override public function set scaleY(param1:Number) : void
        {
            background.scaleY = param1;
            refresh();
            return;
        }// end function

        public function dispose() : void
        {
            maskTimer.stop();
            return;
        }// end function

        public function set brightness(param1:Number) : void
        {
            _brightness = MathUtil.unit(param1);
            param1 = param1 * 100;
            var _loc_2:Array = [1, 0, 0, 0, param1, 0, 1, 0, 0, param1, 0, 0, 1, 0, param1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1];
            video.filters = [new ColorMatrixFilter(_loc_2)];
            return;
        }// end function

        public function get orginalVideoScale() : Number
        {
            return _orginalVideoScale;
        }// end function

        public function set videoScale(param1:Number) : void
        {
            if (isNaN(param1) || param1 <= 0)
            {
                return;
            }
            _videoScale = param1;
            refresh();
            return;
        }// end function

        override public function get height() : Number
        {
            return background.height;
        }// end function

        private function addMask(param1:uint) : void
        {
            var _loc_2:BitmapData = null;
            var _loc_3:Bitmap = null;
            if (param1 >= 1 && param1 <= 4)
            {
                _loc_2 = new BitmapData(MASK_WIDTH, MASK_HEIGHT, true, 16764159);
                _loc_3 = new Bitmap(_loc_2);
                maskArr.push(_loc_3);
                switch(param1)
                {
                    case 1:
                    {
                        _loc_3.x = 0;
                        _loc_3.y = 0;
                        break;
                    }
                    case 2:
                    {
                        _loc_3.x = video.width / video.scaleX - MASK_WIDTH;
                        _loc_3.y = 0;
                        break;
                    }
                    case 3:
                    {
                        _loc_3.x = video.width / video.scaleX - MASK_WIDTH;
                        _loc_3.y = video.height / video.scaleY - MASK_HEIGHT;
                        break;
                    }
                    case 4:
                    {
                        _loc_3.x = 0;
                        _loc_3.y = video.height / video.scaleY - MASK_HEIGHT;
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                masksContainer.addChild(_loc_3);
                _loc_3.filters = [new BlurFilter(BLURXY, BLURXY)];
                if (!maskTimer.running)
                {
                    maskTimer.start();
                }
            }
            return;
        }// end function

        public function setBackground(param1:uint, param2:Number) : void
        {
            var _loc_3:* = background.width;
            var _loc_4:* = background.height;
            RectDisplayUtil.drawRect(background.graphics, _loc_3, _loc_4, param1, param2);
            return;
        }// end function

        private function addRectMask(param1:Rectangle) : void
        {
            var _loc_2:* = new BitmapData(param1.width, param1.height, true, 0);
            var _loc_3:* = new Bitmap(_loc_2);
            _loc_3.x = param1.x;
            _loc_3.y = param1.y;
            rectMaskArr.push(_loc_3);
            masksContainer.addChild(_loc_3);
            _loc_3.filters = [new BlurFilter(BLURXY, BLURXY)];
            if (!maskTimer.running)
            {
                maskTimer.start();
            }
            return;
        }// end function

        public function get rotateType() : int
        {
            return _rotateType;
        }// end function

        private function renderMask(event:TimerEvent) : void
        {
            var bmp:Bitmap;
            var bmpData:BitmapData;
            var matrix:Matrix;
            var i:int;
            var te:* = event;
            var allMaskArr:* = maskArr.concat(rectMaskArr);
            try
            {
                i;
                while (i < allMaskArr.length)
                {
                    
                    bmp = Bitmap(allMaskArr[i]);
                    bmpData = bmp.bitmapData;
                    matrix = new Matrix();
                    matrix.tx = -bmp.x;
                    matrix.ty = -bmp.y;
                    bmpData.draw(video, matrix);
                    i = (i + 1);
                }
            }
            catch (e:Error)
            {
                trace("Can not draw Video！", i, e, e.getStackTrace());
                if (_playURL)
                {
                    maskTimer.stop();
                }
            }
            return;
        }// end function

        public function setMasks(param1:Array) : void
        {
            var _loc_2:int = 0;
            var _loc_3:* = undefined;
            maskArr = [];
            rectMaskArr = [];
            while (masksContainer.numChildren > 0)
            {
                
                masksContainer.removeChildAt(0);
            }
            masksContainer.addChild(masksContainer.mask);
            maskTimer.stop();
            if (param1 == null || param1.length == 0)
            {
                hasLogo = false;
            }
            else
            {
                hasLogo = true;
                _loc_2 = 0;
                while (_loc_2 < param1.length)
                {
                    
                    _loc_3 = param1[_loc_2];
                    if (_loc_3 is Rectangle)
                    {
                        addRectMask(_loc_3);
                    }
                    else
                    {
                        addMask(_loc_3);
                    }
                    _loc_2++;
                }
            }
            return;
        }// end function

        override public function get width() : Number
        {
            return background.width;
        }// end function

        override public function setWidthHeight(param1:Number, param2:Number) : void
        {
            background.width = param1;
            background.height = param2;
            refresh();
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            background.height = param1;
            refresh();
            return;
        }// end function

        override public function reset() : void
        {
            setMasks(null);
            rotateType = 0;
            super.reset();
            return;
        }// end function

        public function set fullFillVideo(param1:Boolean) : void
        {
            _fullFillVideo = param1;
            refresh();
            return;
        }// end function

        override public function get scaleY() : Number
        {
            return background.scaleY;
        }// end function

        public function get brightness() : Number
        {
            return _brightness;
        }// end function

        override public function onMetaData(param1:Object) : void
        {
            if (_metaData)
            {
                return;
            }
            super.onMetaData(param1);
            _orginalVideoScale = _metaData.width / _metaData.height;
            if (isNaN(_orginalVideoScale) || _orginalVideoScale <= 0)
            {
                _orginalVideoScale = 4 / 3;
            }
            videoScale = _orginalVideoScale;
            return;
        }// end function

        override protected function createNetStream() : void
        {
            super.createNetStream();
            netStream.checkPolicyFile = hasLogo || _alwaysCheckPolicyFile;
            return;
        }// end function

        protected function refresh() : void
        {
            video.rotation = 0;
            var _loc_1:* = background.width;
            var _loc_2:* = background.height;
            this.mask.width = _loc_1;
            this.mask.height = _loc_2;
            var _loc_3:* = _loc_1 / _loc_2;
            var _loc_4:* = _videoScale;
            var _loc_5:String = "width";
            var _loc_6:String = "height";
            if (_rotateType % 2 != 0)
            {
                _loc_4 = 1 / _loc_4;
                _loc_5 = "height";
                _loc_6 = "width";
            }
            var _loc_7:* = _loc_4 >= _loc_3;
            _loc_7 = _fullFillVideo ? (!_loc_7) : (_loc_7);
            if (_loc_7)
            {
                video[_loc_5] = _loc_1;
                video[_loc_6] = _loc_1 / _loc_4;
                video.rotation = _rotateType * 90;
                video.x = 0;
                video.y = (_loc_2 - video.height) / 2;
            }
            else
            {
                video[_loc_6] = _loc_2;
                video[_loc_5] = _loc_2 * _loc_4;
                video.rotation = _rotateType * 90;
                video.y = 0;
                video.x = (_loc_1 - video.width) / 2;
            }
            if (_rotateType == 1)
            {
                video.x = video.x + video.width;
            }
            else if (_rotateType == 2)
            {
                video.x = video.x + video.width;
                video.y = video.y + video.height;
            }
            else if (_rotateType == 3)
            {
                video.y = video.y + video.height;
            }
            masksContainer.rotation = 0;
            masksContainer[_loc_5] = video.width;
            masksContainer[_loc_6] = video.height;
            masksContainer.rotation = video.rotation;
            masksContainer.x = video.x;
            masksContainer.y = video.y;
            return;
        }// end function

        override public function get scaleX() : Number
        {
            return background.scaleX;
        }// end function

        public function get alwaysCheckPolicyFile() : Boolean
        {
            return _alwaysCheckPolicyFile;
        }// end function

        public function get fullFillVideo() : Boolean
        {
            return _fullFillVideo;
        }// end function

        public function set rotateType(param1:int) : void
        {
            if (param1 >= 0 && param1 <= 3)
            {
                _rotateType = param1;
                refresh();
            }
            return;
        }// end function

        override public function set filters(param1:Array) : void
        {
            video.filters = param1;
            return;
        }// end function

        override public function close() : void
        {
            setMasks(null);
            rotateType = 0;
            super.close();
            return;
        }// end function

        override public function get filters() : Array
        {
            return video.filters;
        }// end function

    }
}
